/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.style.BulletSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.android.google.apis.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class MapViewCompassDemo extends MapActivity {

	final int MAPZOOM = 18;
	final Handler mHandler = new Handler();
	final Runnable mUpdateResults = new Runnable() {
		public void run() {
			Toast.makeText(
					MapViewCompassDemo.this,
					"Latitude: " + currentLocation.getLatitudeE6()
							+ " Longitude: " + currentLocation.getLongitudeE6(),
					Toast.LENGTH_SHORT).show();
		}
	};
	private static final String TAG = "Game screeen";
	private SensorManager mSensorManager;
	private MapView mMapView;
	private MyLocationOverlay mMyLocationOverlay;
	private GeoPoint currentLocation;
	private List<Overlay> listOfOverlays;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMapView = new MapView(this, getString(R.string.apiKey));
		setContentView(mMapView);
		mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
		mMyLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				mMapView.getController().animateTo(
						mMyLocationOverlay.getMyLocation());
				currentLocation = mMyLocationOverlay.getMyLocation();
				mHandler.post(mUpdateResults);
			}
		});
		
		mMapView.getOverlays().add(mMyLocationOverlay);
		mMapView.getController().setZoom(MAPZOOM);
		mMapView.setClickable(true);
		mMapView.setEnabled(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mMyLocationOverlay.enableMyLocation();
	}

	@Override
	protected void onStop() {
		mMyLocationOverlay.disableMyLocation();
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.optionsmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		BuildingsOverlay buildingsOverlay = new BuildingsOverlay();
		currentLocation = mMyLocationOverlay.getMyLocation();
		switch (item.getItemId()) {
		case R.id.fortressbutton:		
			if (currentLocation != null) {
				mHandler.post(mUpdateResults);
				buildingsOverlay.setPointToDraw(currentLocation);
				buildingsOverlay.setResourceIdToDraw(R.drawable.fortress64);
				mMapView.getOverlays().add(buildingsOverlay);
			}
			break;
		case R.id.hospitalbuton:
			if (currentLocation != null) {
				mHandler.post(mUpdateResults);
				buildingsOverlay.setPointToDraw(currentLocation);
				buildingsOverlay.setResourceIdToDraw(R.drawable.redcross64);
				mMapView.getOverlays().add(buildingsOverlay);
			}
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}